<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        body{
            height: 3000px;
        }
    </style>
</head>
<body>

<script>
    /**
     * 获取滚动的头部距离和左边距离
     * scroll().top scroll().left
     * @returns {*}
     */
    function scroll() {
        if(window.pageYOffset !== null){
            return {
                top: window.pageYOffset,
                left: window.pageXOffset
            }
        }else if(document.compatMode === "CSS1Compat"){ // W3C
            return {
                top: document.documentElement.scrollTop,
                left: document.documentElement.scrollLeft
            }
        }

        return {
            top: document.body.scrollTop,
            left: document.body.scrollLeft
        }
    }

    window.onscroll = function () {
        console.log(scroll().top);
    }
</script>
</body>
</html>